package com.yanqu.road.server.manger.player;

import com.yanqu.road.dao.impl.player.InternalUserDaoImpl;
import com.yanqu.road.entity.enums.eChannelType;
import com.yanqu.road.entity.player.InternalUserIpLocation;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.gameplayer.module.player.PlayerModule;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.pb.PlayerPb;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class WhiteListLocationMgr extends TempMgr {

    private static Map<Long, InternalUserIpLocation> ipLocationMap = new ConcurrentHashMap<>();

    public static boolean whiteNoSetIpLocation(long userId) {
        ServerInfo serverInfo = ServerListMgr.getServerInfo(GameServer.getInstance().getServerId());
        if (serverInfo == null) {
            return false;
        }
        if (!eChannelType.isMainLandChannel(serverInfo.getChannelId())) {
            return false;
        }
        InternalUserIpLocation ipLocation = ipLocationMap.get(userId);
        if (ipLocation == null) {
            return false;
        }
        if (StringUtils.isNullOrEmpty(ipLocation.getIpLocation())) {
            return true;
        }
        return false;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        long serverId = GameServer.getInstance().getServerId();
        ipLocationMap = new InternalUserDaoImpl().getInternalUserIpLocation(serverId);
        for (InternalUserIpLocation userIpLocation : ipLocationMap.values()) {
            long userId = userIpLocation.getUserId();
            UserInfo userInfo = UserMgr.getUserInfo(userId);
            if (userInfo != null) {
                String location = userIpLocation.getIpLocation();
                if (!StringUtils.isNullOrEmpty(location)) {
                    userInfo.setIpLocation(location);
                    GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
                    if (player != null) {
                        player.getModule(PlayerModule.class);
                        PlayerProto.PlayerDataTempMsg.Builder msg = PlayerPb.parsePlayerDataTempMsg(userInfo, player.getModule(AttributeModule.class).getUserAttributeMap());
                        if (null != msg) {
                            player.sendPacket(Protocol.U_PLAYER_DATA, msg);
                        }
                    }
                }
            }
        }
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static String getIpLocation(long userId) {
        InternalUserIpLocation ipLocation = ipLocationMap.get(userId);
        if (ipLocation != null) {
            String location = ipLocation.getIpLocation();
            if (!StringUtils.isNullOrEmpty(location)) {
                return location;
            }
        }
        return null;
    }

    public static boolean isWhiteLocationUser(long userId) {
        return ipLocationMap.containsKey(userId);
    }
}
